package kom.gui.interfaces;

import java.awt.Color;
import java.util.*;
import kom.Kom;
import kom.gfx.Screen;

public abstract class Window {

    protected boolean closeable = true;
    protected boolean fixed;
    protected int startX = 999;
    protected int startY = 999;
    protected int x;
    protected int y;

    public int getTelaX() {
        return x;
    }

    public void setTelaX(int telaX) {
        this.x = telaX;
    }

    public int getTelaY() {
        return y;
    }

    public void setTelaY(int telaY) {
        this.y = telaY;
    }
    protected int sizeX;
    protected int sizeY;
    protected int oldX; //old X
    protected int oldY; // old Y
    protected int newX, newY; // new X and new Y
    protected String name;
    protected List<Button> buttons = new ArrayList<Button>();
    boolean ativa;

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

    public int getMouseX() {
        return this.oldX;
    }

    public int getMouseY() {
        return this.oldY;
    }

    public void setMouseX(int x) {
        this.oldX = x;
    }

    public void setMouseY(int y) {
        this.oldY = y;
    }

    public void render(Screen screen) {
        //if(grafico!=null)
//        g.setColor(Color.YELLOW);
//        g.fillRect(x, y, sizeX,sizeY);
//        g.setColor(Color.BLACK);
//        g.drawRect(x, y, sizeX,sizeY);
//        renderExtras(g);

        screen.drawRect(x, y, sizeX, sizeY, true, Color.YELLOW);
        screen.drawRect(x, y, sizeX, sizeY, false, Color.BLACK);
    }

    public String getNome() {
        return this.name;
    }

    public void setActive(boolean b) {
        this.ativa = b;
    }

    public int getMaxX() {
        return this.sizeX;
    }

    public int getMaxY() {
        return this.sizeY;
    }

    public boolean clickedMe(int x, int y) {
        if (x > this.x && x < this.x + sizeX) {
            if (y > this.y && y < this.y + sizeY) {
                return true;
            }
        }
        return false;
    }

    public String clickedMyButton(int x, int y) {
        for (int sx = 0; sx < buttons.size(); sx++) {
            if (x > buttons.get(sx).getX() + this.x && x < buttons.get(sx).getX() + this.x + buttons.get(sx).getSizeX()) {
                if (y > buttons.get(sx).getY() + this.y && y < buttons.get(sx).getY() + this.y + buttons.get(sx).getSizeY()) {
                    return buttons.get(sx).getAcao();
                }
            }
        }
        return "none";
    }

    public void renderButtons(Screen screen) {
        for (int xx = 0; xx < buttons.size(); xx++) {
            buttons.get(xx).desenhaBotao(screen, x, y);
        }
    }

    public abstract void update();

    public void addButton(Button qual) {
        this.buttons.add(qual);
        for (Button b : buttons) {
            if (b.getSelected() > 0) {
                return;
            }
        }
        buttons.get(0).setSelected(1);
    }

    public void updateWindowPosition() {
        if (this.fixed) {
            return;
        }

        x += newX - oldX;
        y += newY - oldY;
        oldY = y - (y - newY);
        oldX = x - (x - newX);

        if (x < 0) {
            x = 0;
        }
        if (x + sizeX > Kom.width) {
            x = Kom.width - sizeX;
        }

        if (y < 0) {
            y = 0;
        }
        if (y + sizeY > Kom.height) {
            y = Kom.height - sizeY;
        }

    }

    public void setMXY(int x, int y) {

        // int difX = x - oldX;
        // int difY = y - oldY;
        this.newX = x;//-difX;
        this.newY = y;//-difY;

    }

    public void trataMenu(int dir) {
    }

    public String acaoMenu() {
        return "";
    }
}
